<?php

function tournaments_handle_add_page($org_guid = 0)
{	
	if(!$org_guid)
		forward();
	
	$org = get_entity($org_guid);
	
	$content = elgg_view('frontend/add/add', array('org_entity' => $org));
	$sidebar = elgg_view('sidebar/tournament', array("entity" => null));
	
	$params = array(
		'content' => $content,
		'sidebar' => $sidebar,
		'title' => $org->title.": Crear torneo",
		'filter' => '',
	);
	$body = elgg_view_layout('content', $params);

	echo elgg_view_page($org->title, $body);
}
function tournaments_handle_edit_page($guid = 0)
{
	elgg_set_page_owner_guid($guid);
	if(!$guid)
		forward();
	
	$entity = get_entity($guid);
	
	tournaments_register_edit_buttons($entity);
	$content = elgg_view('frontend/edit/edit', array('entity' => $entity));
	$sidebar = elgg_view('sidebar/tournament', array('entity' => $entity));
	
	$params = array(
		'content' => $content,
		'sidebar' => $sidebar,
		'title' => $entity->title,
		'filter' => '',
	);
	$body = elgg_view_layout('one_column', $params);

	echo elgg_view_page($org->title, $body);
}

function tournaments_handle_view_page($guid)
{
	elgg_set_page_owner_guid($guid);
	$entity = get_entity($guid);
	
	tournaments_register_view_buttons($entity);
	$content = elgg_view('frontend/view/view', array("entity" => $entity));
	$sidebar = elgg_view('sidebar/tournament', array("entity" => $entity));
	
	$params = array(
		'content' 	=> $content,
		'sidebar' 	=> $sidebar,
		'title' 	=> $entity->title,
		'filter' 	=> '',
	);
	$body = elgg_view_layout('content', $params);

	echo elgg_view_page($org->title, $body);
}

function tournaments_register_view_buttons($entity)
{	
	if($entity->isManager())
	{
		elgg_register_menu_item('title', array(
		'name' => "tournaments_edit",
		'href' => $CONFIG->url."tournaments/edit/".$entity->guid,
		'text' => elgg_echo("tournaments:edit"),
		'link_class' => 'elgg-button elgg-button-action',));
		
		elgg_register_menu_item('title', array(
			'name' => "tournaments_manager",
			'href' => $CONFIG->url."tournaments/manager/".$entity->guid,
			'text' => elgg_echo("tournaments:access_manager"),
			'link_class' => 'elgg-button elgg-button-action',));
	}
	else
	{
		elgg_register_menu_item('title', array(
		'name' => "tournaments_sign_up",
		'href' => "#",
		'text' => elgg_echo("tournaments:sign_up"),
		'link_class' => 'elgg-button elgg-button-special',));
	}
}

function tournaments_register_edit_buttons($entity)
{
	elgg_register_menu_item('title', array(
		'name' => "tournaments_view",
		'href' => $CONFIG->url."tournaments/view/".$entity->guid,
		'text' => elgg_echo("tournaments:view"),
		'link_class' => 'elgg-button elgg-button-action',));
}

?>